package net.zhengxinyun.performance.service.login;

import net.zhengxinyun.performance.Util.MD5;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.mapper.login.UserEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: hyp
 * @Date: 2018/10/30 14:39
 */
@Service
public class UserServiceImpl implements UserService {

    private UserEntityMapper userEntityMapper;

    @Autowired
    public UserServiceImpl(UserEntityMapper userEntityMapper) {
        this.userEntityMapper = userEntityMapper;
    }

    @Override
    public int delete(UserEntity userEntity) {
        return userEntityMapper.deleteByPrimaryKey(userEntity.getId());
    }

    @Override
    public int update(UserEntity userEntity) throws Exception {
        if (StringUtils.isNotBlank(userEntity.getUserPassword())){
            UserEntity entity = userEntityMapper.selectByPrimaryKey(userEntity.getId());
            if (!entity.equals(userEntity.getUserPassword())){
                String passWord = MD5.md5(userEntity.getUserPassword(),MD5.MD5_STR);
                userEntity.setUserPassword(passWord);
            }
        }
        return userEntityMapper.updateByPrimaryKey(userEntity);
    }

    @Override
    public int insert(UserEntity userEntity) {
        return userEntityMapper.insert(userEntity);
    }

    @Override
    public List<UserFilter> query(UserFilter userFilter) {
        return userEntityMapper.query(userFilter);
    }

    @Override
    public UserEntity queryById(Long id) {
        return userEntityMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer queryForCount(UserFilter userFilter) {
        return userEntityMapper.queryForCount(userFilter);
    }

    @Override
    public List<String> queryForDepartment() {
        return userEntityMapper.queryForDepartment();
    }


}
